<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryRef" :inline="false" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="出库状态" prop="orderStatus">
              <el-radio-group v-model="queryParams.orderStatus" @change="handleQuery">
                <el-radio-button :key="-2" :label="-2">全部</el-radio-button>
                <el-radio-button
                    v-for="item in wms_receipt_status"
                    :key="item.value"
                    :label="item.value"
                >
                  {{ item.label }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="出库类型" prop="optType">
              <el-radio-group v-model="queryParams.optType" @change="handleQuery">
                <el-radio-button :key="-1" :label="-1">全部</el-radio-button>
                <el-radio-button
                    v-for="item in wms_receipt_type"
                    :key="item.value"
                    :label="item.value"
                >
                  {{ item.label }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="出库单号" prop="orderNo">
              <el-input
                  v-model="queryParams.orderNo"
                  placeholder="请输入出库单号"
                  clearable
                  @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="mt20">
      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="6"><span style="font-size: large">出库单据</span></el-col>
        <el-col :span="1.5">
          <el-button
              type="primary"
              @click="handleAdd"
          >新增
          </el-button>
        </el-col>
      </el-row>
      <el-table :data="outboundList" border class="mt20" height="calc(100vh - 320px)" @expand-change="handleExpandChange">
        <!-- 单据明细 -->
        <el-table-column type="expand" width="40">
          <template #default="{ row }">
            <div style="padding: 0 50px 20px 50px">
              <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
                <el-col :span="6"><span style="font-size: large">单据明细</span></el-col>
              </el-row>
              <!-- 修改此处的数据绑定，使用 row.detailData 获取对应明细数据 -->
              <el-table :data="row.detailData || []" border>
                <el-table-column show-overflow-tooltip type="index" label="行号" align="center"></el-table-column>
                <el-table-column show-overflow-tooltip prop="drugId" label="药品编码" align="center" width="150"/>
                <el-table-column show-overflow-tooltip prop="drugName" label="药品名称" align="center" width="300"/>
                <el-table-column show-overflow-tooltip prop="drugCd" label="生产厂家" align="center"/>
                <el-table-column show-overflow-tooltip prop="DrugPZWH" label="批准文号" align="center" width="150"/>
                <el-table-column show-overflow-tooltip prop="drugBatchNo" label="批号" align="center" width="120"/>
                <el-table-column show-overflow-tooltip prop="drugGg" label="规格" align="center" width="200"/>
                <el-table-column show-overflow-tooltip prop="drugDw" label="单位" align="center" width="80"/>
                <el-table-column show-overflow-tooltip prop="drugNum" label="数量" align="center" width="80"/>
                <el-table-column show-overflow-tooltip prop="drugCheckNum" label="确认数量" align="center" width="100"/>
                <el-table-column show-overflow-tooltip prop="drugPd" label="生产日期" align="center" width="120"/>
                <el-table-column show-overflow-tooltip prop="drugExp" label="有效期" align="center" width="120"/>
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="billsId" label="单据号" align="center" width="150"></el-table-column>
        <el-table-column show-overflow-tooltip prop="billsTypeName" label="单据类型" align="center" width="120"></el-table-column>
        <el-table-column show-overflow-tooltip prop="billsCdName" label="往来单位" align="center" width="200"></el-table-column>
        <el-table-column show-overflow-tooltip prop="totalPackages" label="总件数" align="center" width="100"></el-table-column>
        <el-table-column show-overflow-tooltip prop="totalQuantity" label="总数量" align="center" width="100"></el-table-column>
        <el-table-column show-overflow-tooltip label="单据状态" align="center" width="130">
          <template #default="scope">
            <el-tag v-if="scope.row.billsState === '0'" type="success">已出库</el-tag>
            <el-tag v-else-if="scope.row.billsState === '1'" type="warning">未出库</el-tag>
            <el-tag v-else-if="scope.row.billsState === '2'" type="danger">已作废</el-tag>
            <el-tag v-else>未知状态</el-tag>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="creatorName" label="制单人" align="center" width="100"></el-table-column>
        <el-table-column show-overflow-tooltip prop="createTime" label="制单时间" align="center" width="160"></el-table-column>
        <el-table-column show-overflow-tooltip prop="checkDate" label="确认时间" align="center" width="160"></el-table-column>
        <el-table-column show-overflow-tooltip prop="userId" label="操作人" align="center" width="100"></el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template #default="scope">
            <el-button v-if="scope.row.billsState === '1'" type="primary" size="small" @click="handleEdit(scope.row)">修改</el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="float: right; margin: 20px">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryParams.pageNum"
                       :page-sizes="[10, 50, 100]" :page-size="queryParams.pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  finishStoreList,//获取列表
  finishStoreDetail,//获取详情
  finishStoreListDelete,//删除单据
} from '@/api/storage/finishStore'

export default {
  name: 'WmsOutbound',
  data() {
    return {
      loading: true,
      total: 0,
      outboundList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: '',//出库单号
        orderStatus: -2,//出库状态
        optType: -1,//出库类型
      },
      wms_receipt_status: [
        {
          label: '已出库',
          value: 0
        },
        {
          label: '未出库',
          value: 1
        },
        {
          label: '作废',
          value: 2
        }
      ],//审核类型
      wms_receipt_type: [
        { label: '销售出库', value: 'SalesWareHouseOut'},
        { label: '退货出库', value: 'ReturnWareHouseOut'},
        { label: '调拨出库', value: 'AllocateWareHouseOut'},
        { label: '返工出库', value: 'ReworkWareHouseOut'},
        { label: '销毁出库', value: 'DestoryWareHouseOut'},
        { label: '抽检出库', value: 'CheckWareHouseOut'},
        { label: '直调出库', value: 'DirectAllocateWareHouseOut'},
        { label: '赠品出库', value: 'GiftHouseOut'},
        { label: '其他出库', value: 'OtherHouseOut'},
        { label: '使用出库', value: 'RetailWareHouse0ut'}
      ],//出库类型
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      finishStoreList(this.queryParams).then(response => {
        this.outboundList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.getList()
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        orderNo: '',//出库单号
        orderStatus: -2,//出库状态
        optType: -1,//出库类型
      }
      this.handleQuery()
    },
    handleAdd() {
      this.$router.push({
        path: "/storage/finishStore/qmsDocument"
      })
    },
    handleEdit(row) {
      this.$router.push({
        path: "/storage/finishStore/qmsDocument",
        query: {
          billsId: row.billsId
        }
      })
    },
    handleSizeChange(newPageSize) {
      this.queryParams.pageSize = newPageSize
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleCurrentChange(newPage) {
      this.queryParams.pageNum = newPage
      this.getList()
    },
    //详情
    handleExpandChange(row, expandedRows){
      // 检查当前行是否在展开列表中（表示正在展开）
      const isExpanded = expandedRows.some(item => item.billsId === row.billsId);

      // 如果正在展开且没有数据，则请求数据
      if (isExpanded && (!row.detailData || row.detailData.length === 0)) {
        finishStoreDetail(row.billsId).then(res=>{
          // 使用 Vue.set 或者 this.$set 来确保响应式
          this.$set(row, 'detailData', res.data.outDetailList);
        }).catch(err => {
          this.$message.error('获取明细数据失败：' + err.message);
        })
      }
    },
    handleDelete(row) {
      this.$confirm('确认删除该出库单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        finishStoreListDelete(row.billsId).then(res => {
          this.$message.success(res.msg)
          this.getList()
        })
      }).catch(() => {
        this.$message.error('用户取消')
      })
    }
  }
}
</script>
<style scoped>
.mt20 {
  margin-top: 20px;
}
.mb10 {
  margin-bottom: 10px;
}
.mb8 {
  margin-bottom: 8px;
}
</style>
